package xgenerator.ui.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import xgenerator.constants.JavaTypes;
import xgenerator.ui.swing.adapter.ConstraintTypeComboBoxItemListener;
import xgenerator.ui.swing.adapter.MetadataTableModelListener;
import xgenerator.ui.swing.adapter.MetadataTableRowAction;
import xgenerator.ui.swing.adapter.TableColumnComboBoxPopupMenuListener;
import xgenerator.ui.swing.adapter.TableComboBoxPopupMenuListener;
import xgenerator.ui.swing.model.ComponentTypeComboBoxModel;
import xgenerator.ui.swing.model.ConstraintTypeComboBoxModel;
import xgenerator.ui.swing.model.SqlDataTypeComboBoxModel;
import xgenerator.ui.swing.model.PropertyTypeComboBoxModel;
import xgenerator.ui.swing.model.MetadataTableModel;
import xgenerator.ui.swing.model.QueryModeComboBoxModel;
import xgenerator.ui.swing.model.DeleteRuleComboBoxModel;

/**
 * <p>
 * Title：MetadataTable
 * </p>
 * <p>
 * Description：
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see 
 * @since 1.0
 */
public class MetadataTable extends JTable implements UIConstants {
	
	/**
	 * 表格行高
	 */
	public static final int ROW_HEIGHT = 30;
	
	/**
	 * 表格行选择背景色
	 */
	public static final Color SELECTION_BACKGROUND_COLOR = new Color(207, 228, 249);
	
	/**
	 * 表格行选择前景色
	 */
	public static final Color SELECTION_FOREGROUND_COLOR = new Color(0, 0, 0);
	
	/**
	 * 表格颜色
	 */
	public static final Color GRID_COLOR = new Color(236, 233, 216);
	
	/**
	 * UI组件
	 */
	private Component parentComponent;

	/**
	 * constructor
	 * @param parentComponent
	 */
	public MetadataTable(Component parentComponent) {
		this.parentComponent = parentComponent;
		this.initComponents();
	}

	/**
	 * <p>
	 * Title：初始化表格组件
	 * </p>
	 * 
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 */
	private void initComponents() {
		this.setPreferredScrollableViewportSize(new Dimension(550, 30));
		this.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		this.setAutoCreateRowSorter(true);
		this.setRowHeight(ROW_HEIGHT);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//		this.setColumnSelectionAllowed(true);  //设置表格的的选取以单元格为单位
		this.setSelectionBackground(SELECTION_BACKGROUND_COLOR);
		this.setSelectionForeground(SELECTION_FOREGROUND_COLOR);
		this.setGridColor(GRID_COLOR);
//		this.setBackground(Color.RED);
//		this.setForeground(Color.YELLOW);
//		this.getSelectionModel().setSelectionInterval(1, 1);
//		this.getSelectionModel().setSelectionInterval(3, 1);
//		this.getSelectionModel().setSelectionInterval(5, 1);
//		this.setShowVerticalLines(false);
//		this.setShowHorizontalLines(false);
//		this.setShowGrid(false);

		/*
		 * 表格上的弹出菜单
		 */
		this.setComponentPopupMenu(buildTablePopupMenu());

		/*
		 * 必须先设置TableModel，然后再设置TableColumnModel
		 */
		this.setModel(new MetadataTableModel());
		this.setPreferredTableColumnStyle();

		/*
		 * 添加TableModelListener
		 */
		this.getModel().addTableModelListener(new MetadataTableModelListener(this));

		this.updateUI();
	}
	
	/**
	 * <p>
	 * Title：设置列样式
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 */
	public void setPreferredTableColumnStyle() {
		TableColumnModel tableColumnModel = this.getColumnModel(); // 列模型

		TableColumn column = null;
		for (int i = 0; i < tableColumnModel.getColumnCount(); i++) {
			column = tableColumnModel.getColumn(i);
			if (MetadataTableModel.COMMENTS_INDEX == i) {// 注释
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.SQL_DATA_TYPE_INDEX == i) {// SQL数据类型
				JComboBox sqlDataTypeComboBox = new JComboBox(new SqlDataTypeComboBoxModel());
				column.setCellEditor(new DefaultCellEditor(sqlDataTypeComboBox));
				column.setPreferredWidth(220);
			} else if (MetadataTableModel.DATA_LENGTH_INDEX == i) {// 数据长度
				column.setPreferredWidth(100);
			} else if (MetadataTableModel.DATA_PRECISION_INDEX == i) {// 精度
				column.setPreferredWidth(100);
			} else if (MetadataTableModel.PROPERTY_TYPE_INDEX == i) {// 属性数据类型
				JComboBox propertyTypeComboBox = new JComboBox(new PropertyTypeComboBoxModel(new JavaTypes()));
				propertyTypeComboBox.setEditable(true);
				column.setCellEditor(new DefaultCellEditor(propertyTypeComboBox));
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.NULLABLE_INDEX == i) {// isEmpty
				column.setPreferredWidth(120);
			}  else if (MetadataTableModel.DATA_DEFAULT_INDEX == i) {// defaultValue
				column.setPreferredWidth(130);
			} else if (MetadataTableModel.IS_KEY_FIELD_INDEX == i) {// isKey
				column.setPreferredWidth(70);
			} else if (MetadataTableModel.CONSTRAINT_TYPE_INDEX == i) {// keyType
				JComboBox constraintTypeComboBox = new JComboBox(new ConstraintTypeComboBoxModel());
				constraintTypeComboBox.addItemListener(new ConstraintTypeComboBoxItemListener(this));
				column.setCellEditor(new DefaultCellEditor(constraintTypeComboBox));
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.REFERENCING_TABLE_INDEX == i) {// ReferencingTable
				JComboBox referencingTableComboBox = new JComboBox();
				referencingTableComboBox.addPopupMenuListener(new TableComboBoxPopupMenuListener(this));
				column.setCellEditor(new DefaultCellEditor(referencingTableComboBox));
//				column.setCellRenderer(new DefaultTableCellRenderer());
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.REFERENCING_COLUMN_INDEX == i) {// ReferencingTableColumn
				JComboBox referencingTableColumnComboBox = new JComboBox();
				referencingTableColumnComboBox.addPopupMenuListener(new TableColumnComboBoxPopupMenuListener(this));
				column.setCellEditor(new DefaultCellEditor(referencingTableColumnComboBox));
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.DELETE_RULE_INDEX == i) {// Referential Triggered Action
				JComboBox deleteRuleComboBox = new JComboBox(new DeleteRuleComboBoxModel());
				column.setCellEditor(new DefaultCellEditor(deleteRuleComboBox));
				column.setPreferredWidth(250);
			} else if (MetadataTableModel.IS_QUERY_FIELD_INDEX == i) {// queryField
				column.setPreferredWidth(120);
			} else if (MetadataTableModel.QUERY_MODE_INDEX == i) {// queryMode
				JComboBox queryModeComboBox = new JComboBox(new QueryModeComboBoxModel());
				column.setCellEditor(new DefaultCellEditor(queryModeComboBox));
				column.setPreferredWidth(200);
			} else if (MetadataTableModel.COMPONENT_TYPE_INDEX == i) {// componentType
				JComboBox componentTypeComboBox = new JComboBox(new ComponentTypeComboBoxModel());
				componentTypeComboBox.setRenderer(new ComponentTypeListCellRenderer());
				column.setCellEditor(new DefaultCellEditor(componentTypeComboBox));
				column.setPreferredWidth(250);
			} else {
				column.setPreferredWidth(200);
			}
		}
	}

	/**
	 * <p>
	 * Title：构建表格上的弹出菜单
	 * </p>
	 * 
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JPopupMenu buildTablePopupMenu() {
		JPopupMenu popupMenu = new JPopupMenu("弹出菜单");

		JMenuItem menuItem = null;
		
		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("复制注释->显示名称");
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row_insert.png"));
		menuItem.setActionCommand(MetadataTableRowAction.COPY_COMMENT_TO_DISPLAY_NAME);
		popupMenu.add(menuItem);
		
		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("复制全部注释->显示名称");
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row_insert.png"));
		menuItem.setActionCommand(MetadataTableRowAction.COPY_ALL_COMMENT_TO_DISPLAY_NAME);
		popupMenu.add(menuItem);
		
		popupMenu.add(new JSeparator());
		
		JMenu menu = new JMenu("插入(I)");
		menu.setMnemonic('B');
		
		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("在上方插入行(A)");
		menuItem.setMnemonic('A');
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row_insert.png"));
		menuItem.setActionCommand(MetadataTableRowAction.INSERT_ROW_ABOVE_CURRENT_ROW_COMMAND);
		menu.add(menuItem);

		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("在下方插入行(B)");
		menuItem.setMnemonic('B');
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row_insert.png"));
		menuItem.setActionCommand(MetadataTableRowAction.INSERT_ROW_BELOW_CURRENT_ROW_COMMAND);
		menu.add(menuItem);
		
		popupMenu.add(menu);
		

		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("添加行(A)");
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row.png"));
		menuItem.setActionCommand(MetadataTableRowAction.INSERT_ROW_AT_BOTTOM_COMMAND);
		popupMenu.add(menuItem);
		
		popupMenu.add(new JSeparator());

		menuItem = new JMenuItem(new MetadataTableRowAction(this));
		menuItem.setText("删除行(D)");
		menuItem.setIcon(IconUtils.createImageIcon(ICON_CLASS_PATH + "table_row_delete.png"));
		menuItem.setActionCommand(MetadataTableRowAction.DELETE_ROW_COMMAND);
		menuItem.setEnabled(true);
		popupMenu.add(menuItem);

		return popupMenu;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public Component getParentComponent() {
		return parentComponent;
	}

}
